package com.sunlg.smarthome.contract.presenter;

import com.sunlg.smarthome.base.App;
import com.sunlg.smarthome.base.C;
import com.sunlg.smarthome.contract.NotificationContract;
import com.sunlg.smarthome.data.entity.Notification;
import com.sunlg.smarthome.data.remote.MySubscriber;
import com.sunlg.smarthome.data.remote.Result;
import com.sunlg.smarthome.data.remote.SHServiceWrapper;
import com.sunlg.smarthome.utils.PreferHelper;

import java.util.List;
import java.util.concurrent.TimeoutException;

/**
 * Created by sunlg on 2017/5/30.
 */

public class NotificationPresenter implements NotificationContract.Presenter {

    private NotificationContract.View mView;
    private SHServiceWrapper mSHServiceWrapper;
    private PreferHelper preferHelper;

    public NotificationPresenter() {
        mSHServiceWrapper = App.getInstance().getServiceWrapper();
        preferHelper = App.getInstance().getPreferHelper();
    }

    @Override
    public void loadAllNotification() {
        mSHServiceWrapper.getAllNotification(preferHelper.getInt(C.PreKey.USER_ID, 0),
                new MySubscriber<Result<List<Notification>>>() {
                    @Override
                    public void onSuccess(Result<List<Notification>> data) {
                        if(data.getCode() == C.ActionStatus.SUCCESS){
                            mView.loadSuccess(data.getData());
                        }else {
                            _onError(new Throwable(data.getMsg()));
                        }
                    }

                    @Override
                    public void _onError(Throwable e) {
                        mView.showError(e.getMessage());
                    }
                });
    }

    @Override
    public void attachView(NotificationContract.View view) {
        mView = view;
    }

    @Override
    public void dettachView() {
        mView = null;
    }
}
